package server.views;

import java.awt.Container;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException;
import javax.swing.JFrame;
import javax.swing.JTabbedPane;
//import sjtu.DCL.GM.server.goodsmng.GoodsMng;
import server.utils.ServerUtils;
import server.views.servermngpanel.ServerMngPanel;
import server.views.servermngpanel.toppanel.TopPanel;
import server.views.usermngpanel.UserMngPanel;
import server.config.Public;

/**
 * 服务器的主窗体
 * @author solomon
 */
public class ServerMain extends JFrame {

	private static final long serialVersionUID = -7685165352708010613L;

	private JTabbedPane jtp;				//存放面板的容器 
	private UserMngPanel userMngPanel ;		//用户管理面板
	private ServerMngPanel serverMngPanel;	//服务及在线用户管理面板
//	private GoodsMng goodsMng;				//商品信息管理

	public static long getSerialVersionUID() {
		return serialVersionUID;
	}

	/*	public GoodsMng getGoodsMng() {
		return goodsMng;
	}*/

	/*	public void setGoodsMng(GoodsMng goodsMng) {
		this.goodsMng = goodsMng;
	}*/

	public JTabbedPane getJtp() {
		return jtp;
	}

	public void setJtp(JTabbedPane jtp) {
		this.jtp = jtp;
	}

	public ServerMngPanel getServerMngPanel() {
		return serverMngPanel;
	}

	public void setServerMngPanel(ServerMngPanel serverMngPanel) {
		this.serverMngPanel = serverMngPanel;
	}

	public UserMngPanel getUserMngPanel() {
		return userMngPanel;
	}

	public void setUserMngPanel(UserMngPanel userMngPanel) {
		this.userMngPanel = userMngPanel;
	}

	public ServerMain() {
		//窗体属性
		this.setTitle("聊天服务器");
		this.setSize(650, 500);
		this.setResizable(false);
		this.setLocationRelativeTo(null);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		//各页的面板
		serverMngPanel=new ServerMngPanel(this);
		userMngPanel=new UserMngPanel(this);
//		goodsMng=new GoodsMng(this);

		//实例化选项卡
		jtp = new JTabbedPane();
		jtp.add("服务及在线用户管理",serverMngPanel);
		jtp.add("用户信息管理",userMngPanel);
//		jtp.add("商品信息管理",goodsMng);

		//取得容器
		Container container = this.getContentPane();
		container.add(jtp);
		this.addWindowListener(new WindowAdapter()
		{
			public void windowClosing(WindowEvent e) 
			{
				//窗体关闭时执行的动作，执行和关闭服务一样的动作
				Public.stop = true;// 终止线程

				Public.serverClose();
				//更新显示在服务器端的上线用户信息
				TopPanel.setTShow("",8);
				TopPanel tp = new TopPanel();
				tp.writeLog();

				try {
					if (Public.serverSocket != null)
						Public.serverSocket.close();
				} catch (IOException e1) {
					return;
				} catch (Exception e1) {
					return;
				}
				//清空Public.mySocketInfo里存的客户端的信息
				Public.clientThreads.clear();
				Public.clientSocket.clear();
				Public.clientIStream.clear();
				Public.clientOStream.clear();

				System.exit(0);
			}
		});
		ServerUtils.setLAF(ServerUtils.properties.getProperty("DefaultLF"), this);
	}

	/**
	 * 服务器端主入口
	 * @param args
	 */
	public static void main(String[] args) {
		ServerUtils.setSelfFont("宋体");
		(new ServerMain()).setVisible(true);
	}

}
